﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using PPI.General;

namespace PPI.Entities
{
    public class EBaseImage : EBasePosition
    {
        #region private members
        private Int32 m_Id;
        private FileInfo m_ImageFile;
        private string m_fileName;
        protected string m_TypeName;
        #endregion

        #region Properties

        public Int32 Id
        {
            get
            {
                return m_Id;
            }
            set
            {
                m_Id = value;
            }
        }

        public FileInfo ImageFile
        {
            get { return m_ImageFile; }
            set { m_ImageFile = value; }
        }

        public string Title
        {
            get
            {
                if (Id == Int32.MinValue)
                {
                    if (ImageFile != null)
                        return "Unsaved " + ImageFile.Name;
                    else
                        return "Unsaved Image";
                }
                else
                    return "Image_" + Id.ToString();
            }
        }

        public string FileName
        {
            get { return m_fileName; }
            set { m_fileName = value; }
        }

        #endregion

        #region Constructors

        public EBaseImage()
            : base()
        {
            m_Id = Int32.MinValue;
        }

        public EBaseImage(Int32 id)
            : this()
        {
            m_Id = id;
        }

        #endregion

        #region Utilities
        public static byte[] ToBytes(string fileName)
        {
            FileStream _FileStream = null;
            BinaryReader _BinaryReader = null;
            try
            {
                _FileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                _BinaryReader = new BinaryReader(_FileStream);
                byte[] ImageBytes = _BinaryReader.ReadBytes((int)_FileStream.Length);
                return ImageBytes;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                _BinaryReader.Close();
                _FileStream.Close();
                _FileStream.Dispose();
            }
        }

        #endregion

        #region Virtual Methods

        protected override string GetPlaceMark()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Placemark id=\"" + GetPlacemarkId() + "\">");
            s.AppendLine(" <Style>");
            s.AppendLine("     <BalloonStyle>");
            s.AppendLine("         <color>F9DDCD</color>");
            s.AppendLine("         <text>$[description]</text>");
            s.AppendLine("     </BalloonStyle>");
            s.AppendLine("</Style>");
            s.AppendLine(" <description>");
            s.AppendLine("     <![CDATA[" + GetHtmlPlacemark() + "]]>");
            s.AppendLine(" </description>");
            s.AppendLine("</Placemark>");
            return s.ToString();
        }

        public virtual string GenerateHTML()
        {
            throw new NotImplementedException();

        }

        public string GetImageUrl(Int32 parentId)
        {
            ConfigReader reader = ConfigReader.GetInstance();
            string url = "http://www.dev-poleposition.com/" + reader.ConectionMode + "/Images/" + m_TypeName + "/" + GetDirectoryName(parentId) + "/" + m_fileName;
            return url;
        }

        public string GetTemporaryFolder()
        {
            return PPI.Constants.K_RepositoryFolder + "\\" + PPI.Constants.TempFolderName + "\\" + m_TypeName +"\\"+ FileName;
        }

        public string GetDirectoryName(Int32 parentId)
        {
            int Start = 0;
            int End = 99;
            string _Directory = "";
            bool _FoundPosition = false;
            while (!_FoundPosition)
            {
                if (parentId > Start && parentId > End)
                {
                    Start += 100;
                    End += 100;
                }
                else
                {
                    _FoundPosition = true;
                    _Directory = Start.ToString() + "-" + End.ToString();
                }
            }
            return _Directory;
        }

        #endregion

        #region Override

        public override bool Equals(object obj)
        {
            if (obj is EBaseImage)
            {
                EBaseImage i = obj as EBaseImage;
                // Return true if the fields match:
                return (m_Id == i.m_Id);
            }
            else
            {
                return false;
            }
        }

        #endregion

    }
}
